/*
 * ======================
 * EasyINI v0.1 alpha
 * ======================
 *
 * Created by: Jones
 */
 
class EasyINI
{
	lastsec = null;
	sections = {};
	filename = "";
	constructor(fname)
	{
		filename = fname;
		fileCreate(fname);
		local tf = eFile(fname, "r");
		while(!tf.eos())
		{
			local line = tf.readline();
			local secReg = regexp(@"\[[\C]+]");
			local res = secReg.search(line);
			if(res != null)
			{
				if(findSection(line.slice(res.begin+1, res.end-1)) == null)
				{
					createSection(line.slice(res.begin+1, res.end-1));
					lastsec = sections[sections.len()-1];
				}
				else
				{
					lastsec = findSection(line.slice(res.begin+1, res.end-1));
				}
				continue;
			}
			if(lastsec == null)
			{
				continue;
			}
			secReg = regexp("=");
			res = secReg.search(line);
			if(res != null)
			{
				setKey(lastsec.secname, line.slice(0, res.begin), line.slice(res.end));
			}			
		}
	}
	/*
	 * Internal Methods
	 */
	function createSection(name)
	{
		local tab = {secname = name}
		sections[sections.len()] <- tab;
	}
	function findSection(name)
	{
		foreach(s in sections)
		{
			if(s != null && s.secname == name)
			{
				return s;
			}
		}
		return null;
	}
	function createKey(section, key, val)
	{
		local s = findSection(section);
		if(s != null)
		{
			local tab =
			{
				keyname = key
				keyval = val
			};
			s[s.len()] <- tab;
			return true;
		}
		else
		{
			createSection(section);
			local s = findSection(section);
			if(s == null)
			{
				log("Some serious shit happened. Tell the scripter about that.");
				return false;
			}
			local tab =
			{
				keyname = key
				keyval = val
			};
			s[s.len()] <- tab;
			return true;
		}
		return false;
	}
	function findKey(section, key)
	{
		local s = findSection(section);
		if(s == null)
		{
			return null;
		}
		foreach(k in s)
		{
			if(typeof k == "table")
			{
				if(k.keyname == key)
				{
					return k;
				}
			}				
		}
		return null;
	}
	/*
	 * Public Methods
	 */
	function setKey(section, key, val)
	{
		if(val == null)
		{
			throw "EasyINI Exception: Value must not be null!";
		}
		local k = findKey(section, key);
		if(k == null)
		{
			createKey(section, key, val);
		}
		else
		{
			k.keyval = val;
		}
	}
	function getKey(section, key)
	{
		local k = findKey(section, key);
		if(k == null)
		{
			return null;
		}
		return k.keyval;		
	}
	function deleteKey(section, key)
	{
		local s = findSection(section);
		if(s == null)
		{
			return false;
		}
		foreach(i, k in s)
		{
			if(typeof k == "table")
			{
				if(k.keyname == key)
				{
					s[i] = null;
					return true;
				}
			}
		}
		return false;
	}
	function deleteSection(name)
	{
		foreach(i,s in sections)
		{
			if(s != null && s.secname == name)
			{
				sections[i] = null;
				return true;
			}
		}
		return false;
	}
	function logData()
	{
		foreach(s in sections)
		{
			if(s != null)
			{
				log("[" + s.secname + "]");
				foreach(k in s)
				{
					if(typeof k == "table")
					{
						log(k.keyname + "=" + k.keyval);
					}
				}
			}
		}
	}
	function reloadData()
	{
		sections.clear();
		local tf = eFile(fname, "r");
		lastsec = null;
		while(!tf.eos())
		{
			local line = tf.readline();
			local secReg = regexp(@"\[[\C]+]");
			local res = secReg.search(line);
			if(res != null)
			{
				if(findSection(line.slice(res.begin+1, res.end-1)) == null)
				{
					createSection(line.slice(res.begin+1, res.end-1));
					lastsec = sections[sections.len()-1];
				}
				else
				{
					lastsec = findSection(line.slice(res.begin+1, res.end-1));
				}
				continue;
			}
			if(lastsec == null)
			{
				continue;
			}
			secReg = regexp("=");
			res = secReg.search(line);
			if(res != null)
			{
				setKey(lastsec.secname, line.slice(0, res.begin), line.slice(res.end));
			}	
		}		
	}
	function saveData()
	{
		local tf = eFile(filename, "w");
		tf.writeline(";C'Admin Info File generated by EasyINI");
		foreach(s in sections)
		{
			if(s != null)
			{
				tf.writeline("[" + s.secname + "]");
				foreach(k in s)
				{
					if(typeof k == "table")
					{
						tf.writeline(k.keyname + "=" + k.keyval);
					}
				}
			}
		}		
	}
}

function fileCreate(filename)
{
	local tf = file(filename, "a+");
	tf = null;
	return true;
}

class eFile extends file
{
	function readline()
	{
		local result = "";
		while(!this.eos())
		{
			local c = this.readn('c');
			if(c == '\n' || !c)
			{
				return result;
			}
			if(c == '\r')
			{
				continue;
			}
			result += c.tochar();
		}
		return result;
	}
	function writeline(line)
	{
		foreach(char in line)
		{
			this.writen(char, 'c');
		}
		this.writen('\r', 'c');
		this.writen('\n', 'c');
	}
}

function fileExists(filename)
{
	local tf = file(filename, "a+");
	if(!tf.len())
	{
		tf = null;
		remove(filename);
		return false;
	}
	return true;
}